<template>
    <Container>
        <div class="container-header group" slot="header">
            <Input
                class="start"
                v-model.trim="name"
                placeholder="请输入活动名称"
                @on-enter="getData">
            </Input>
            <Switch v-model="flag"></Switch>
            <Button type="primary" icon="ios-search" class="btn-search" @click="getData"></Button>
        </div>
        <Tabs v-model="tab">
            <Button
              size="large"
              class="item"
              type="primary"
              slot="extra"
              icon="md-add"
              @click="onAddAct">新增活动</Button>
            <TabPane label="抽奖设置" name="tab1">
              <homeTable
                :list="list"
                @getData="getData"
                :hasPage="false"/>
            </TabPane>
        </Tabs>
    </Container>
</template>
<script>
import homeTable from "~/homeTable"
import LottListData from "./edit/LotteryEditData"
import { clearEmpty } from '@/util'
export default {
  name: 'sales/lottery/edit',
	components: {
    homeTable
	},
  data() {
    return {
      tab: "tab1",
      list: LottListData,
      usedDepartment: [],
      flag: true
    };
  },
  mounted() {
    this.$bus.$on("onEdit", this.onEdit);
    this.$bus.$on("onDelete", this.onDelete);
    this.$bus.$on('onChange', ({row, v}) => {this.onChange(row, v)})
		this.getData();
  },
  destroyed() {
    this.$bus.$off();
  },
  methods: {
    getData() {
			let param = {
        name: this.name
      }
      param = clearEmpty(param)
      this.list.status = "loading"
      this.$ajaxGet("/lottery/rule/page", param).then(data => {
        this.list.data = []
        this.list.data = (data.records || []).map(item => {
          return {
            ...item
          }
        })
        this.list.data.forEach(item => {
          this.usedDepartment = this.usedDepartment.concat(item.departmentIdSet)
        })
        this.list.status = "pass"
      }).catch(() => {  
        this.list.status = "error"
      })
		},
    onAddAct() {
      this.$router.push({
        name: 'sales/lottery/info'
      })
		},
		onEdit(id, selfDepartmentId) {
			this.$router.push({
        name: 'sales/lottery/info',
        query: {
          id
        }
      })
		},
    onDelete(id, name, index) {
			this.$Modal.confirm({
        title: "删除",
        content: "确定删除当前活动？",
        onOk: () => {
          this.$ajaxPost(`/lottery/rule/del/${id}`).then(data => {
            this.$Notice.success({
              title: "删除成功",
              desc: `<span class="font-red">${name}</span> 已被删除。`
            });
            this.list.data.splice(index, 1)
            this.usedDepartment = this.list.data.map(item => item.departmentId)
          })
        }
      });
    },
    onChange(row, v) {
      let url = row.isEnable ? `/lottery/rule/disabled/${row.id}` : `/lottery/rule/enabled/${row.id}`
      this.$ajaxPost(url).then(() => {
        this.list.data[row._index].isEnable = v
        this.$Notice.success({
          title: '修改成功'
        })
      }).catch(() => {
        this.$nextTick(() => {
          let newData = JSON.parse(JSON.stringify(this.list.data))
          this.list.data = []
          setTimeout(() => {
            this.$set(this.list, 'data', newData)
          }, 10)
        })
      })
    }
	}
};
</script>
